package com.wutong.eams.config;

import com.wutong.eams.exception.DatabaseException;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * JDBC数据库连接池配置
 *
 * @author wutong
 * @since 21
 */
@Slf4j
public class DatabaseConfig {

    // 数据库配置
    private static final String BASE_URL = "jdbc:mysql://localhost:3306/";
    private static final String DB_NAME = "eams";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    // 连接池配置
    private static final int MAX_POOL_SIZE = 10;
    private static final int MIN_IDLE = 3;
    private static final long CONNECTION_TIMEOUT = 30000;
    private static final long IDLE_TIMEOUT = 600000;
    private static final long MAX_LIFETIME = 1800000;

    @Getter
    private static final DataSource dataSource;

    static {
        try {
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl(buildConnectionUrl());
            config.setUsername(USERNAME);
            config.setPassword(PASSWORD);
            config.setDriverClassName("com.mysql.cj.jdbc.Driver");
            config.setMaximumPoolSize(MAX_POOL_SIZE);
            config.setMinimumIdle(MIN_IDLE);
            config.setConnectionTimeout(CONNECTION_TIMEOUT);
            config.setIdleTimeout(IDLE_TIMEOUT);
            config.setMaxLifetime(MAX_LIFETIME);
            config.addDataSourceProperty("cachePrepStmts", "true");
            config.addDataSourceProperty("prepStmtCacheSize", "250");
            config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
            config.addDataSourceProperty("useServerPrepStmts", "true");
            config.addDataSourceProperty("useLocalSessionState", "true");
            config.addDataSourceProperty("rewriteBatchedStatements", "true");
            config.addDataSourceProperty("cacheResultSetMetadata", "true");
            config.addDataSourceProperty("cacheServerConfiguration", "true");
            config.addDataSourceProperty("elideSetAutoCommits", "true");
            config.addDataSourceProperty("maintainTimeStats", "false");

            dataSource = new HikariDataSource(config);
            log.info("数据库连接池初始化成功");
        } catch (Exception e) {
            log.error("数据库连接池初始化失败", e);
            throw new DatabaseException("数据库连接池初始化失败: " + e.getMessage());
        }
    }

    private static String buildConnectionUrl() {
        return BASE_URL + DB_NAME +
                "?useUnicode=true" +
                "&characterEncoding=utf8" +
                "&allowMultiQueries=true" +
                "&useSSL=false" +
                "&allowPublicKeyRetrieval=true" +
                "&autoReconnect=true" +
                "&serverTimezone=Asia/Shanghai";
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}
